package com.cy.pj.goods;

import com.cy.pj.goods.pojo.Goods;
import com.cy.pj.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class controller {
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/doFindObject")
    public String doFindObject(String name,Model model){
        List<Goods> list = goodsService.findObject(name);
        model.addAttribute("list", list);
        return "goods";
    }


    @RequestMapping("/updateGoods")
    public String updateGoods(Goods goods,Model model){
        goodsService.updateGoods(goods);
//        List<Goods> list = goodsService.findGoods();
//        model.addAttribute("list", list);
//        return "goods";
        return "redirect:/doFindGoods";
    }

    @RequestMapping("/doFindById/{id}")
    public String doFindById(@PathVariable Integer id, Model model ){
        Goods goods = goodsService.findById(id);
        model.addAttribute("goods", goods);
        return "goods-update";
    }

    @GetMapping("doGoodsAddUI")
    public String doGoodsAddUI(Goods goods,Model model){

        return "goods-add";
    }
    @RequestMapping ("/doInsertGoods")
    public String doInsertGoods(Goods goods,Model model){
        goodsService.insertGoods(goods);
        List<Goods> list = goodsService.findGoods();
        model.addAttribute("list", list);

        return "goods";
    }

    @GetMapping("/doFindGoods")
    public String doFindgoods (Model model){
        List<Goods> list = goodsService.findGoods();
        model.addAttribute("list", list);
        return "goods";
    }
    @RequestMapping("/doDeleteById")
    public String doDeleteById(Integer id,Model model){
        goodsService.deleteById(id);
        List<Goods> list = goodsService.findGoods();
        model.addAttribute("list", list);
        return "goods";
    }

}
